<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'进货发票明细':'进货发票明细'"
    :visible="visible"
    width="80%"
    :fullscreen="true"
    :destroy-on-close="true"
    :lock-scroll="false"
    @open="init"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      style="margin-top: -20px"
      label-width="92px">
      <el-row>
        <el-col :lg="8" :md="12">
          <el-form-item label="供应商编码:">
            <el-input
              disabled
              v-model="form.custcode"
              placeholder=""/>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
          <el-form-item label="发票代码:">
            <el-input
              disabled
              v-model="form.vinvoicedm"
              placeholder="输入发票代码"/>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
          <el-form-item label="发票日期:">
            <el-input
              disabled
              v-model="form.dinvoicedate"
              placeholder="输入发票日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="8" :md="12">
          <el-form-item label="供应商名称:">
            <el-input
              disabled
              v-model="form.custname"
              placeholder=""/>
          </el-form-item>
          </el-col>
        <el-col :lg="8" :md="12">
          <el-form-item label="发票号:">
            <el-input
              disabled
              v-model="form.vinvoicecode"
              placeholder=""/>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
          <el-form-item label="状态:">
            <el-input
              disabled
              v-model="form.statusdesc"
              placeholder="输入备注说明"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <!-- 发票明细数据表格 -->
    <ele-pro-table
      ref="table"
      :datasource="invList"
      :columns="columns"
      :selection.sync="selection"
      :pageSize="100"
      :need-page="false"
      :toolbar="false"
      height="200px">
      <template slot="tbdm" slot-scope="{row}">
        <el-input :disabled="form.status=='20'" v-model="row.zxspbm"></el-input>
      </template>
      <template slot="scph" slot-scope="{row}">
        <el-input :disabled="form.status=='20'" v-model="row.scph"></el-input>
      </template>
      <!-- 表头工具栏 -->
      <template slot="toolbar">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          class="ele-btn-icon"
          @click="addList()"
          :disabled="form.status=='20'">添加明细
        </el-button>
        <el-button
          size="small"
          type="warning"
          icon="el-icon-save"
          class="ele-btn-icon"
          @click="delList()"
          :disabled="form.status=='20'">删除明细
        </el-button>
      </template>
      <!-- 操作列 -->
      <!-- 操作列 -->
      <template slot="action" slot-scope="{row}">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-upload2"
          @click="openEdit(row)"
        >NC回写
        </el-link>
        <el-link
          type="success"
          slot="reference"
          :underline="false"
          icon="el-icon-document"
          @click="openLog(row)"
        >日志
        </el-link>
        <el-link v-if="false"
                 type="primary"
                 :underline="false"
                 icon="el-icon-search"
                 @click="openDetail(row)"
        >单据明细
        </el-link>

      </template>
    </ele-pro-table>
    <!-- 发票上报日志列表-->
    <ele-pro-table
      ref="tableLog"
      :where="where"
      :datasource="logList"
      :columns="columnsLog"
      :toolbar="false"
      :need-page="false"
      height="calc(100vh - 400px)">
      <!-- 表头工具栏 -->
      <template slot="toolbar" v-if="false">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          class="ele-btn-icon"
          @click="openEdit(null)"
        >NC回写
        </el-button>
        <el-button
          size="small"
          type="danger"
          icon="el-icon-delete"
          class="ele-btn-icon"
          @click="removeBatch"
          v-if="false"
        >删除
        </el-button>
      </template>
      <template slot="submitNo" slot-scope="{row}">
        <div style="white-space:pre-wrap">{{ row.yqid }}</div>
      </template>
      <template slot="submitTime" slot-scope="{row}">
        <div style="white-space:pre-wrap">{{ row.createDate }}</div>
      </template>
      <template slot="reqBody" slot-scope="{row}">
        <el-input type="textarea" :rows="3" :value="row.postxml"></el-input>
      </template>
      <template slot="resBody" slot-scope="{row}">
        <el-input type="textarea" :rows="3" :value="row.retpostxml"></el-input>
      </template>
      <template slot="resResult" slot-scope="{row}">
        <el-input type="textarea" :rows="3" :value="row.responseResult"></el-input>
      </template>
    </ele-pro-table>
    <div slot="footer">
      <el-button @click="updateVisible(false)">关闭</el-button>
      <el-button
        type="primary"
        @click="save"
        v-if="false"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import {mapGetters} from "vuex";

export default {
  name: 'SyInvoicDetail',
  computed: {
    ...mapGetters(["permission"]),
  },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      url:'/yyq005/postlog',
      where:{},
      // 表格列配置
      columns: [
        {
          prop: 'sxh',
          label: '序号',
          showOverflowTooltip: true,
          minWidth: 50,
          align: 'left'
        },
        {
          prop: 'invcode',
          label: '存货编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'zxspbm',
          label: '统编代码',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
        {
          prop: 'invname',
          label: '存货名称',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left',
        },
        {
          prop: 'vproducenum',
          label: '批号',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
        },
        {
          prop: 'ninvoicenum',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'right',
        },
        {
          prop: 'nmoney',
          label: '本币无税金额',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'right',
        },
        {
          prop: 'nsummny',
          label: '本币价税合计',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'right',
        },
        {
          prop: 'ntaxmny',
          label: '本币税额',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'right',
        },
        {
          prop: 'norgnettaxprice',
          label: '原币净含税单价',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'right',
        },
        {
          prop: 'noriginalcurprice',
          label: '原币无税净单价',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'right',
        },
        //   {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 160,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      columnsLog: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          prop: 'yqid',
          label: '提交编号',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          slot: 'submitNo'
        },
        {
          prop: 'postxml',
          label: '提交XML',
          showOverflowTooltip: true,
          minWidth: 400,
          align: 'center',
          slot:'reqBody'
        },
        {
          prop: 'retpostxml',
          label: '返回XML',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          slot:'resBody'
        },
        {
          prop: 'ztcljg',
          label: '处理结果',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'retmemo',
          label: '提示内容',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'createDate',
          label: '提交时间',
          showOverflowTooltip: true,
          minWidth: 70,
          align: 'center',
          slot: 'submitTime'
        },
        {
          prop: 'createUsraccount',
          label: '提交人',
          showOverflowTooltip: true,
          minWidth: 70,
          align: 'center',
        },

      ],

      // 表单验证规则
      rules: {
      },
      invList:[],
      logList:[],
      selection:[],
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /*打开窗口是加载明细*/
    init(){
      this.$http.get('/dinvoiceinfo/detail?cinvoiceid='+this.form.cinvoiceid).then(res => {
        if (res.data.code === 0) {
          //加载明细列表
          this.invList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
      this.$http.get('/yyq005/postlog?postid='+this.form.cinvoiceid).then(res => {
        if (res.data.code === 0) {
          //加载阳光日志列表
          this.logList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });

    },
    addList(){
      let item={pruchinvoiceid:'',zxspbm:'',scph: ''};
      this.invList.push(item);

    },
    delList(){
      if(this.invList.length==0) return;
      if(this.selection.length==0){
        this.$message.error('请选择一条需要删除的明细');
        return;
      }
      this.invList.splice(this.invList.findIndex((t) => t.zxspbm === this.selection[0].zxspbm && t.scph === this.selection[0].scph), 1);

    },
    /* 保存编辑 */
    save() {
      if(!this.form.fph)
      {
        this.$message.error('请输入发票号');
        return;
      }
      if(!this.form.fpdm)
      {
        this.$message.error('请输入发票代码');
        return;
      }
      if(!this.form.fprq)
      {
        this.$message.error('请输入发票日期');
        return;
      }
      if(!this.form.fpkjfmc)
      {
        this.$message.error('请输入发票开具方名称');
        return;
      }
      if(!this.form.fpjsfmc)
      {
        this.$message.error('请输入发票接收方名称');
        return;
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          let param = {
            pruchinvoice:this.form,
            pruchinvoiceLines:this.invList
          };
          this.$http.post(this.isUpdate ? '/mpruchinvoice/update' : '/mpruchinvoice/create', param).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
.el-row
{
  margin-bottom: -10px;
}
.el-form-item
{
  margin-bottom: -5px;
}
</style>
